﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anagram
{
    public class Anagrams
    {
        public string baseStringForCreatingAnagrams { get; private set;}
        public List<string> listOfAnagrams { get; private set; }

        public Anagrams(string baseStringForCreatingAnagrams)
        {
            this.baseStringForCreatingAnagrams = baseStringForCreatingAnagrams;
            createAnagrams();
        }

        private void createAnagrams()
        {
            if (this.baseStringForCreatingAnagrams == "")
            {
                listOfAnagrams = new List<string>();
                return;
            }

            if (baseStringForCreatingAnagrams.Length == 1)
            {
                listOfAnagrams = new List<string>();
                this.listOfAnagrams.Add(this.baseStringForCreatingAnagrams);
                return;
            }

            listOfAnagrams = Permutations(this.baseStringForCreatingAnagrams);
        }

        public List<string> Permutations(string stringToPermutate)
        {
            List<string> myPermutations = new List<string>();

            for(int i=0; i<stringToPermutate.Length; i++)
            {
                string newStringToPermutate = stringToPermutate.Remove(i,1);

                if (newStringToPermutate == "")
                {
                    myPermutations.Add(stringToPermutate);
                }
                else 
                {
                    List<string> subPermutations = Permutations(newStringToPermutate);
                    foreach (string subPermutation in subPermutations)
                    {
                        myPermutations.Add(stringToPermutate[i] + subPermutation);
                    }
                }
                
                
            }
            
            return myPermutations;
        }
    }
}
